package actions;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import service.VehicleService;
import domain.Vehicle;
/**
 * @author system auto
 */
public class VehicleUpdateAction extends Action {
	/**
	 * 用于查询车辆.
	 */
	private VehicleService vehicleService = null;
	/**
	 * 修改车辆.
	 * @param mapping action mapping
	 * @param form action form
	 * @param request http request
	 * @param response http response
	 * @return action forword
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	       HttpServletRequest request, HttpServletResponse response)
	       throws Exception {
	   	// 验证是否是有效的Token,防止重复提交
	   	if (!isTokenValid(request, true)) {
	    	// 重复提交,转到重复提交失败界面
	    	return mapping.findForward("error.duplicateSubmit");
	   	}
		try{
			DynaValidatorForm vehicleForm = (DynaValidatorForm) form;
		 	String id = vehicleForm.getString("id");
			Vehicle vehicle = vehicleService.getVehicleById(id);
			this.convertBean(vehicle,vehicleForm);
	       	vehicleService.updateVehicle(vehicle);
	    }catch(Exception e){
	        ActionErrors errors=new ActionErrors();
	        errors.add("errors.data.operate.error", new ActionMessage("errors.data.operate.error",utils.DaoUtil.exceptionToStr(e)));
	        this.addErrors(request, errors);
	           // 设置token 防止重复提交
	        this.saveToken(request);
	        return mapping.getInputForward();
	    }
	    return mapping.findForward("success");
    }
    /**
     *convertBean
     */
     private void convertBean(Vehicle vehicle, DynaValidatorForm vehicleForm)
	 		throws Exception{
		String numCode=vehicleForm.getString("numCode");
		vehicle.setNumCode(numCode);
		String manufactType=vehicleForm.getString("manufactType");
		vehicle.setManufactType(manufactType);
		String carType=vehicleForm.getString("carType");
		vehicle.setCarType(carType);
		String registNumber=vehicleForm.getString("registNumber");
		vehicle.setRegistNumber(registNumber);
		String carAge=vehicleForm.getString("carAge");
		if(carAge!=null&&!carAge.equals("")){
			vehicle.setCarAge(Integer.valueOf(carAge));
		}else{
			vehicle.setCarAge(null);
		}
		String shipGRT=vehicleForm.getString("shipGRT");
		if(shipGRT!=null&&!shipGRT.equals("")){
			vehicle.setShipGRT(Double.valueOf(shipGRT));
		}else{
			vehicle.setShipGRT(null);
		}
		String engineNumber=vehicleForm.getString("engineNumber");
		vehicle.setEngineNumber(engineNumber);
		String operaCert=vehicleForm.getString("operaCert");
		vehicle.setOperaCert(operaCert);
		String chargeShipGRT=vehicleForm.getString("chargeShipGRT");
		if(chargeShipGRT!=null&&!chargeShipGRT.equals("")){
			vehicle.setChargeShipGRT(Double.valueOf(chargeShipGRT));
		}else{
			vehicle.setChargeShipGRT(null);
		}
		String buyDate=vehicleForm.getString("buyDate");
		if(buyDate!=null&&!buyDate.equals("")){
			DateFormat DATE_FAMAT_BUYDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
			vehicle.setBuyDate(DATE_FAMAT_BUYDATE.parse(buyDate));
		}else{
			vehicle.setBuyDate(null);
		}
		String carName=vehicleForm.getString("carName");
		vehicle.setCarName(carName);
		String carNote=vehicleForm.getString("carNote");
		vehicle.setCarNote(carNote);
		String carStage=vehicleForm.getString("carStage");
		vehicle.setCarStage(carStage);
		String curPosition=vehicleForm.getString("curPosition");
		vehicle.setCurPosition(curPosition);
		String carStatus=vehicleForm.getString("carStatus");
		vehicle.setCarStatus(carStatus);
		String carVolume=vehicleForm.getString("carVolume");
		if(carVolume!=null&&!carVolume.equals("")){
			vehicle.setCarVolume(Double.valueOf(carVolume));
		}else{
			vehicle.setCarVolume(null);
		}
		String oilRate=vehicleForm.getString("oilRate");
		if(oilRate!=null&&!oilRate.equals("")){
			vehicle.setOilRate(Double.valueOf(oilRate));
		}else{
			vehicle.setOilRate(null);
		}
		String carBodyNumber=vehicleForm.getString("carBodyNumber");
		vehicle.setCarBodyNumber(carBodyNumber);
		String carColor=vehicleForm.getString("carColor");
		vehicle.setCarColor(carColor);
     }
   /**
     * @param pVehicleService the vehicleService to set
     */
    public void setVehicleService(VehicleService pVehicleService) {
        vehicleService = pVehicleService;
    }
}
